package com.demo2do.fms.entity.appraisal;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.format.PersistentFormat;
import com.demo2do.fms.entity.authority.Branch;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 * 绩效考核   
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "FMS_APPRAISAL")
@PersistentFormat
public class Appraisal {
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
    @SequenceGenerator(name = "generator", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1)
    private Long id;
    
    /** 所属考核标准 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @PersistentFormat
    private AppraisalTemplate template;
    
    /** 参与者类型  */
    @Enumerated(EnumType.STRING)
    private AppraisalTarget target;
    
    /** 考核名称 */
    private String name;
    
    /** 总分 */
    private BigDecimal scoreTotal;
    
    /** 开始时间 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginDate;
    
    /** 结束时间 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;
    
    /** 是否完成 */
    @Column(name = "is_finished")
    private boolean finished;
    
    /** 是否是总部创建 */
    @Column(name = "is_admin_create")
    private boolean adminCreate;
    
    /** 创建人所属分区 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JoinColumn(name = "branch_id", referencedColumnName = "id")
    private Branch branch;
    
    /** 创建人所属供方 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    private Supplier supplier;
    
    /** 创建人 */
    private Long creator;
    
    /** 创建时间 */
    private Date createDate;
    
    /** 最后修改人 */
    private Long lastOperator;
    
    /** 最后修改时间 */
    private Date lastModifyDate;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public Appraisal() {
    }
    
    /**
     * @param operator
     * @return
     */
    public Appraisal initialize(Long operator) {
        this.target = template.getTarget();
        this.finished = false;
        this.adminCreate = template.isAdminCreate();
        this.branch = template.getBranch();
        this.supplier = template.getSupplier();
        this.creator = operator;
        this.createDate = new Date();
        this.lastOperator = operator;
        this.lastModifyDate = new Date();
        return this;
    }
    
    /**
     * @param operator
     * @return
     */
    public Appraisal finish(Long operator) {
        this.finished = true;
        this.lastOperator = operator;
        this.lastModifyDate = new Date();
        return this;
    }
    
    /**
     * 
     * @param lastModifyDate
     * @param lastOperator
     * @return
     */
    public Appraisal operate(Long lastOperator) {
        this.lastOperator = lastOperator;
        this.lastModifyDate = new Date();
        return this;
    }
    
    /**
     * 
     * @param originalScore
     * @param score
     * @return
     */
    public Appraisal rate(BigDecimal originalScore, BigDecimal score) {
        BigDecimal originalScoreTotal = this.scoreTotal == null ? BigDecimal.ZERO : this.scoreTotal;
        this.scoreTotal = originalScoreTotal.subtract(originalScore).add(score);
        return this;
    }
    
    /**
     * @param operator
     * @return
     */
    public Appraisal delete(Long operator) {
        this.disabled = true;
        this.lastOperator = operator;
        this.lastModifyDate = new Date();
        return this;
    }

    /**
     * 获取考核类型
     * 
     * @return
     */
    @Transient
    public AppraisalType getAppraisalType() {
        if (adminCreate) {
            return AppraisalType.ADMIN;
        } else if (branch != null) {
            return AppraisalType.BRANCH;
        } else if (supplier != null) {
            return AppraisalType.SPONSOR;
        } else {
            return null;
        }
    }
    
    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the template
     */
    public AppraisalTemplate getTemplate() {
        return template;
    }

    /**
     * @return the target
     */
    public AppraisalTarget getTarget() {
        return target;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the scoreTotal
     */
    public BigDecimal getScoreTotal() {
        return scoreTotal;
    }

    /**
     * @return the beginDate
     */
    public Date getBeginDate() {
        return beginDate;
    }

    /**
     * @return the endDate
     */
    public Date getEndDate() {
        return endDate;
    }

    /**
     * @return the finished
     */
    public boolean isFinished() {
        return finished;
    }

    /**
     * @return the adminCreate
     */
    public boolean isAdminCreate() {
        return adminCreate;
    }

    /**
     * @return the branch
     */
    public Branch getBranch() {
        return branch;
    }

    /**
     * @return the supplier
     */
    public Supplier getSupplier() {
        return supplier;
    }

    /**
     * @return the creator
     */
    public Long getCreator() {
        return creator;
    }

    /**
     * @return the createDate
     */
    public Date getCreateDate() {
        return createDate;
    }

    /**
     * @return the lastOperator
     */
    public Long getLastOperator() {
        return lastOperator;
    }

    /**
     * @return the lastModifyDate
     */
    public Date getLastModifyDate() {
        return lastModifyDate;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param template the template to set
     */
    public void setTemplate(AppraisalTemplate template) {
        this.template = template;
    }

    /**
     * @param target the target to set
     */
    public void setTarget(AppraisalTarget target) {
        this.target = target;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param scoreTotal the scoreTotal to set
     */
    public void setScoreTotal(BigDecimal scoreTotal) {
        this.scoreTotal = scoreTotal;
    }

    /**
     * @param beginDate the beginDate to set
     */
    public void setBeginDate(Date beginDate) {
        this.beginDate = beginDate;
    }

    /**
     * @param endDate the endDate to set
     */
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    /**
     * @param finished the finished to set
     */
    public void setFinished(boolean finished) {
        this.finished = finished;
    }

    /**
     * @param adminCreate the adminCreate to set
     */
    public void setAdminCreate(boolean adminCreate) {
        this.adminCreate = adminCreate;
    }

    /**
     * @param branch the branch to set
     */
    public void setBranch(Branch branch) {
        this.branch = branch;
    }

    /**
     * @param supplier the supplier to set
     */
    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    /**
     * @param creator the creator to set
     */
    public void setCreator(Long creator) {
        this.creator = creator;
    }

    /**
     * @param createDate the createDate to set
     */
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    /**
     * @param lastOperator the lastOperator to set
     */
    public void setLastOperator(Long lastOperator) {
        this.lastOperator = lastOperator;
    }

    /**
     * @param lastModifyDate the lastModifyDate to set
     */
    public void setLastModifyDate(Date lastModifyDate) {
        this.lastModifyDate = lastModifyDate;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
